package com.wxy.controller;

import java.util.Map;

import org.apache.ibatis.ognl.ASTBitNegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxy.config.jwtConfig;
import com.wxy.dao.userMapper;
import com.wxy.domain.User;
import com.wxy.domain.indexUser;
import com.wxy.domain.jwtUser;
import com.wxy.domain.myException;
import com.wxy.service.userService;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/index")
public class indexController {
	@Autowired
	private userService userService;
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response) {
		try {
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			jwtUser user=new jwtUser(username, password);
			User user2=userService.findUser(user);
			String token=jwtConfig.getToken(user2.getId());
			Cookie cookie=new Cookie("token",token);
			response.addCookie(cookie);
			return "success";
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			throw new myException("账号密码错误...");
		}
	}
//	@RequestMapping("/getMessage")
//	@ResponseBody
//	public User getMessage(@RequestHeader(value="userId",required = false)String[] userId) {
//		String idString=userId[0];
//		int id=Integer.parseInt(idString);
//		System.out.println("token里的id为"+id);
//		User user=userService.findById(id);
//		if(user==null)
//			throw new myException("伪造登录...");
//		return user;
// 	}
	@RequestMapping("/getMessage")
	@ResponseBody
	public User getMessage() {
		int id=indexUser.getUserId();
		System.out.println("indexUser里的id为"+id);
		User user=userService.findById(id);
		if(user==null)
			throw new myException("伪造登录...");
		return user;
 	}
}
